package com.module.base.base.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.module.base.base.BaseExceptionViewModel
import java.lang.reflect.Field

abstract class BaseViewHolder<T, DB : ViewDataBinding> :
    BaseNewViewHolder<T> {

    open lateinit var mDatabind: DB

    /**
     * 泛型 布局
     */
    constructor(parent: ViewGroup) : this(1, parent)


    /**
     * 泛型 布局
     */
    protected constructor(index: Int, parent: ViewGroup) : super(parent) {
        mDatabind =
            inflateBindingWithGeneric(index, LayoutInflater.from(parent.context), parent, false)
        modifyItemView(mDatabind?.root)
    }


    /**
     * 支持传统方式，直接传布局
     */
    constructor(parent: ViewGroup, @LayoutRes idRes: Int) : super(parent, idRes)



    /**
     * 修改试图
     */
    @Throws(Exception::class)
    protected fun modifyItemView(newFieldValue: Any) {
        val field: Field = RecyclerView.ViewHolder::class.java.getDeclaredField("itemView")
        if (!field.isAccessible) {
            field.isAccessible = true
        }
        field.set(this, newFieldValue)
    }
}